package com.hjh.pro2.controller;

import com.hjh.pro2.constant.TemplateBean;
import com.hjh.pro2.constant.TemplateProperties;
import com.hjh.pro2.util.VelocityUtils;
import org.apache.velocity.VelocityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private TemplateProperties properties;

    @RequestMapping("/test1")
    public String test() {
        return "hello world";
    }

    @RequestMapping("/test2")
    public String test2() {


        VelocityUtils.getInstance().init();
        String template = "templates/Hellovelocity.vm";

        VelocityContext ctx = new VelocityContext();

        ctx.put("name", "velocity");
        ctx.put("date", (new Date()).toString());

        List<String> temp = new ArrayList();
        temp.add("1");
        temp.add("2");
        ctx.put("list", temp);

        String filePath = "F:\\ttt.java";

        String content = VelocityUtils.getInstance().write2File(template,ctx,filePath);

        return content;
    }


    @RequestMapping("/test3")
    public String test3() {
        String template = "templates/Hellovelocity.vm";

        VelocityContext ctx = new VelocityContext();

        ctx.put("name", "velocity");
        ctx.put("date", (new Date()).toString());

        List<String> temp = new ArrayList();
        temp.add("1");
        temp.add("2");
        ctx.put("list", temp);

        String filePath = "F:\\ttt.java";

        String content = VelocityUtils.getInstance().write2File(template,ctx,filePath);

        return content;
    }

    @RequestMapping("/test4")
    public String test4() {


        String template = "templates/Hellovelocity.vm";

        VelocityContext ctx = new VelocityContext();

        ctx.put("name", "velocity");
        ctx.put("date", (new Date()).toString());

        List<String> temp = new ArrayList();
        temp.add("1");
        temp.add("2");
        ctx.put("list", temp);

        String filePath = "F:\\ttt.java";

        String content = VelocityUtils.getInstance().write2File(template,ctx,filePath);

        return content +"/  "+ properties.getFilter();
    }

    @RequestMapping("/test5")
    public String test5() {


        String template = "templates/Hellovelocity.vm";

        VelocityContext ctx = new VelocityContext();

        ctx.put("name", "velocity");
        ctx.put("date", (new Date()).toString());

        List<String> temp = new ArrayList();
        temp.add("1");
        temp.add("2");
        ctx.put("list", temp);

        String filePath = "F:\\ttt.java";

        String content = VelocityUtils.getInstance().write2File(template,ctx,filePath);

        List<TemplateBean> ss= properties.getList();
        for (TemplateBean s : ss) {
            System.out.println("=========>" +s.getTmplName());
        }

        return content +"/  "+ properties.getFilter();
    }
}
